package com.yy.yyui
{
	import com.maqie.view.UM;
	import com.yy.yyui.table.DataGrid;
	import com.util.tools.GT;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.geom.Rectangle;
	import flash.text.TextField;

	public class DefaultItemRenderer extends Sprite
	{
		private var w:Number,h:Number;
		private var _content:DisplayObject;
		private var adjust:Function;
		private var _data:Object;
		public function DefaultItemRenderer(w:Number,h:Number){
			this.w = w;
			this.h = h;
			adjust = GT.centerScrollChild;
			//this.scrollRect = new Rectangle(0,0,this.width,this.height); 
		}
		
		public function set content(value:Object):void{
			var dobj:DisplayObject;
			if(value==null){
				value = UM.createEmptyDisplayObject();
			}
			if(value is DisplayObject){
				dobj = value as DisplayObject;
			}else if(value is Boolean){
				dobj = UM.visualizeBoolean(value as Boolean);
			}else{
				dobj = UM.visualizeString(value,null,1);
				dobj.cacheAsBitmap = true;
			}
	
			GT.replaceChild(this,dobj,this._content,true,false,false,false);
			this._content = dobj;		
			adjust(this,this._content);
		}
		
		public function set position(value:Object):void{
			switch(value){
				case DataGrid.RIGHT:
					adjust = GT.centerRightScrollChild;
					break;
				case DataGrid.LEFT:
					adjust = GT.centerLeftScrollChild;
					break;
				case DataGrid.CENTER:
				default:
					adjust = GT.centerScrollChild;
			}
			adjust(this,this._content);
		}
		
	
		
		override public function get width():Number{
			return w;
		}
		
		override public function get height():Number{
			return h;
		}
		
		override public function set width(newWidth : Number) : void {
			var diff:Number = newWidth - w;
			var oldW:Number,oldH:Number;
			var i:uint,l:uint;
			if(this._content is TextField){
				this._content.width += diff;
			}
			w = newWidth;
			adjust(this,this._content);
		}
		
		
		
		override public function set height(newHeight : Number) : void {
			var diff:Number = newHeight - h;
			var oldW:Number,oldH:Number;
			var i:uint,l:uint;				
			h = newHeight;
			adjust(this,this._content);					
		}
		
		public function set data(value:Object):void{
			this._data = value;
		}	
		
		public function get data():Object{
			return this._data;
		}
	}
}